<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/
if (!defined('CPG_NUKE')) { exit; }
define('HEADER_OPEN', true);

// temporary patch
if ('HEAD' === $_SERVER['REQUEST_METHOD']) {
	Dragonfly_Net_Http::headersFlush();
	exit;
}

# 1-2 queries
function online()
{
	global $db, $module_title, $SESS, $mainindex;
	if ($SESS->dbupdate) {
		$uname = $SESS->sess_id;
		$guest = 1;
		if (is_user()) {
			$uname = Dragonfly::getKernel()->IDENTITY->nickname;
			$guest = 0;
		} elseif (SEARCHBOT) {
			$uname = SEARCHBOT;
			$guest = 3;
		}
		if (is_admin()) {
			global $CLASS;
			if ($guest == 1) { $uname = $_SESSION['DF_VISITOR']->admin->name; }
			$guest = 2;
		}
		if (empty($uname)) return; # something screwey
		$tbl = $db->TBL->session;
		$data = array(
			'time'   => time(),
			'module' => $module_title ? $module_title : _HOME,
			'url'    => defined('ADMIN_PAGES') ? $mainindex : str_replace('&', '&amp;', URL::uri()),
			'guest'  => $guest,
		);
		$tbl->update($data, array('uname'=>$uname));
		if (false === $db->affected_rows) {
			$data['uname']  = $uname;
			$data['host_addr'] = Dragonfly_Net::ipn();
			$tbl->insert($data);
		}
	}
}

function head()
{
	global $METATAGS, $Blocks, $home, $cpgtpl,
		$bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2,
		$CPG_SESS, $Module;

	define('THEME_PATH', 'themes/' .$CPG_SESS['theme'] .'/');
	header('imagetoolbar: no');
	header(Dragonfly_Net_Http::$contentType['html']);
	$MAIN_CFG = Dragonfly::getKernel()->CFG;
	if ($MAIN_CFG->global->block_frames) {
		header('X-Frame-Options: SAMEORIGIN');
	}

	$header = '';
	$sysMessage = array();
	$userinfo = Dragonfly::getKernel()->IDENTITY;

	if (!$home && $GLOBALS['DF']::BOOT_USER === $GLOBALS['DF']->getState()) {
		if (defined('ADMIN_PAGES') && is_admin() && !(isset($_GET['op']) && $_GET['op'] == 'logout')) {
			Dragonfly_Admin_Menu::display('all', Dragonfly_Admin_Menu::BLOCK | $MAIN_CFG->global->admingraphic);
		}
	}

	$GLOBALS['DF']->setState(DF::BOOT_HEADER);

	if ('msie' == Client::$name && 10 < Client::$version) {
		// Simulate HTML5 Drag&Drop in IE < 10
		Dragonfly_Output_Js::add('includes/poodle/javascript/dragndrop-ie9.js');
	}
	if (defined('ADMIN_PAGES')) {
		if ($MAIN_CFG->global->admin_help) {
			Dragonfly_Output_Js::add('includes/javascript/infobox.js');
		}
	}
	$Blocks = new Blocks($Module->mid);
	$Blocks->prepare(Blocks::LEFT);
	$Blocks->prepare(Blocks::CENTER);
	$Blocks->prepare(Blocks::RIGHT);
	$Blocks->prepare(Blocks::DOWN);

	include_once(THEME_PATH .'theme.php');
	if (!defined('THEME_VERSION')) { define('THEME_VERSION', '9.0'); }

	if ($METATAGS) {
		foreach ($METATAGS as $name => $content) {
			Dragonfly_Page::metatag($name, $content);
		}
	}
	if (!defined('ADMIN_PAGES')) {
		Dragonfly_Page::tag('link rel="canonical" href="'.BASEHREF.URL::canonical().'"');
	}
/*
	$languages = lang_selectbox('', '', false, true);
	for ($i=0; $i < sizeof($languages); $i++) {
		if ($languages[$i] != '') {
			Dragonfly_Page::tag('link rel="alternate" type="text/html" href="?newlang='.$languages[$i].'" hreflang="'._LANGCODE.'" lang="'.get_langcode($languages[$i]).'" title="Version '.ucfirst($languages[$i]).'"';
		}
	}
*/
	if ($Module->name != 'Private_Messages' && is_user() && is_active('Private_Messages') && $userinfo->popup_pm && $userinfo->new_privmsg) {
		Dragonfly_Page::headerData('
<script type="text/javascript">
<!--
	window.open(\''.URL::index('Private_Messages&file=index&mode=newpm', false).'\', \'\', \'height=150,resizable=yes,width=400\');
//-->
</script>');
	}

	if ($MAIN_CFG->global->maintenance) $sysMessage[] = _SYS_MAINTENANCE .' [<a href="'.URL::admin('settings&amp;s=1').'">'._RESET.'</a>]';
	if (is_admin() && DF_MODE_INSTALL) $sysMessage[] = 'Installer mode still active, [<a href="'.URL::admin('settings&amp;s=11').'">'._RESET.'</a>].';
	if (Dragonfly::isDemo()) $sysMessage[] = _SYS_DEMO;
	if (count($sysMessage)) array_unshift($sysMessage, '<strong>'._SYS_MESSAGE.'</strong>');
	$sysMessage = implode('<br />', $sysMessage);
	Dragonfly_Page::setTitle($home ? $MAIN_CFG->global->sitename : Dragonfly_Page::get('title'));
	$cpgtpl->assign_vars(array(
		'BASEHREF'     => BASEHREF,
		'I18N'		     => 'enctype="multipart/form-data"',
		'IMPORTANT_MESSAGE' => $sysMessage,
		'PAGE_TITLE'   => Dragonfly_Page::get('title'), # duplicate
		'S_PAGETITLE'  => Dragonfly_Page::get('title'), # duplicate
		'S_TEXTDIR'	   => _TEXT_DIR,
		'S_LANGCODE'	 => _LANGCODE,
		'S_HEADER_TAGS'=> Dragonfly_Page::getHeaders(),
		'S_SITENAME'   => $MAIN_CFG->global->sitename,
		'S_DELIM'      => _BC_DELIM,
		'S_BLOCK_FRAMES' => intval($MAIN_CFG->global->block_frames),
		'B_SIDE_LEFT'  => $Module->sides & Blocks::LEFT,
		'B_SIDE_CENTER'=> $Module->sides & Blocks::CENTER,
		'B_SIDE_RIGHT' => $Module->sides & Blocks::RIGHT,
		'B_SIDE_DOWN'  => $Module->sides & Blocks::DOWN,
		'B_PAGETITLE'  => 1 < strlen(strip_tags(Dragonfly_Page::get('title'))),
		'S_IS_ADMIN'   => is_admin(),
		'S_CAN_ADMIN'  => can_admin(),
		'IS_ADMIN'     => is_admin(),
		'CAN_ADMIN'    => can_admin(),
		'ADMIN_PAGES'  => defined('ADMIN_PAGES'),
		'S_STATIC_DOMAIN' => DF_STATIC_DOMAIN
	));

	themeheader();
}
//if (300 < $_SESSION['SECURITY']['status']) { online(); }
if (empty($_SESSION['SECURITY']['banned'])) { online(); }
global $home, $cpgtpl, $Module;
head();
if (!defined('ADMIN_PAGES')) {
	require_once('includes/counter.php');  # 2-3 queries
	require_once('includes/functions/messagebox.php');
}

$cpgtpl->display('header');
